package cn.xspace.library.view.nav;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.xspace.android.xafcommon.appliction.BaseApplication;
import com.xspace.android.xafcommon.base.BaseFragment;
import com.xspace.android.xafcommon.been.NavTagBean;
import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.EventBusConstant;
import com.xspace.android.xafcommon.util.Navigation;
import com.xspace.android.xafcommon.util.WorkLogConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xspace.library.R;
import cn.xspace.library.util.BldNavigation;
import cn.xspace.library.util.BuildCache;
import cn.xspace.library.util.BuildConfig;
import cn.xspace.library.view.dialog.PointDialog;
import de.greenrobot.event.EventBus;

/**
 * A simple {@link Fragment} subclass.
 */
public class NavFragment extends BaseFragment implements View.OnClickListener {
    private int mContainerId;
    private FragmentManager mFragmentManager;
    private NavigationButton mCurrentNavButton;
    private OnNavigationReselectListener mOnNavigationReselectListener;
    private LayoutInflater mInflater;

    private NavigationButton navBtn1;
    private NavigationButton navBtn2;
    private NavigationButton navBtn3;
    private ImageView operationImg;
    private NavigationButton navBtn4;
    private NavigationButton navBtn5;
    private List<NavigationButton> buttonList = new ArrayList<>();
    //    private List<BuildConfig.DRAWER_TYLE> gridList;
    PointDialog mDialog = null;


    public NavFragment() {
        // Required empty public constructor
    }

    @Override
    public View initView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_nav, null);
    }

    @Override
    protected void initFindViewById(View view) {
        navBtn1 = (NavigationButton) view.findViewById(R.id.nav_item_1);
        navBtn2 = (NavigationButton) view.findViewById(R.id.nav_item_2);
        operationImg = (ImageView) view.findViewById(R.id.nav_item_img);
        navBtn3 = (NavigationButton) view.findViewById(R.id.nav_item_3);
        navBtn4 = (NavigationButton) view.findViewById(R.id.nav_item_4);
        navBtn5 = (NavigationButton) view.findViewById(R.id.nav_item_5);
//        if (BuildCache.getAppConfigBean(mContext).navigationBean.navStyle == BuildConfig.NavStyle.PONIT_STYLE.getType()) {
//            operationImg.setVisibility(View.VISIBLE);
////            parsePonitList();
//        } else {
        operationImg.setVisibility(View.GONE);
//        }
        initWidget(view);
        navBtn1.setOnClickListener(this);
        navBtn2.setOnClickListener(this);
        operationImg.setOnClickListener(this);
        navBtn3.setOnClickListener(this);
        navBtn4.setOnClickListener(this);
        navBtn5.setOnClickListener(this);
    }

//    private void parsePonitList() {
//        gridList = BuildConfig.getPonitList(mContext);
//    }

    @Override
    public void initData() {

    }


    public void resetBottom() {
        List<NavTagBean> tagList = BuildCache.getAppConfigBean(mContext)
                .navigationBean.navTagList;
        final int size = tagList.size();
        for (int i = 0; i < size; i++) {
            NavigationButton navButon = null;
            NavTagBean navTagBean = tagList.get(i);
            BuildConfig.NativeSkip nativeSkip = null;
            if (BuildConfig.nativeMap.containsKey(navTagBean.navKey)) {
                nativeSkip = BuildConfig.nativeMap.get(navTagBean.navKey);
            }
            if (i == 0) {
                navButon = navBtn1;
            } else if (i == 1) {
                navButon = navBtn2;
            } else if (i == 2) {
                navButon = navBtn3;
            } else if (i == 3) {
                navButon = navBtn4;
            } else if (i == 4) {
                navButon = navBtn5;
            }

//            if (BuildCache.getAppConfigBean(mContext).navigationBean.navStyle == BuildConfig.NavStyle.PONIT_STYLE.getType()) {
//                //底部点聚最对4个tag 1.个iamgevie
//                if (i > 3) {
//                    return;
//                }
//            } else {
            //底部标签最对5个tag
            if (i > 4) {
                return;
            }
//            }

            buttonList.add(navButon);
            navButon.setVisibility(View.VISIBLE);
            navButon.setmUrl(navTagBean.navUrl);
            navButon.setTag(navTagBean);
            Class mCls = null;
            if (nativeSkip == null) {
                return;
            }
            try {
                mCls = Class.forName(nativeSkip.getFragmentClz());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
//            navButon.init(tag_style.getResSelectId(), tag_style.getName(), mCls);
            navButon.init(navTagBean.iconName, navTagBean.navName, mCls);
        }
    }

    protected void initWidget(View root) {
        mInflater = LayoutInflater.from(mContext);
//        List<BuildConfig.DRAWER_TYLE> enumList = BuildConfig.getGridList(mContext);
        List<NavTagBean> tagList = BuildCache.getAppConfigBean(mContext)
                .navigationBean.navTagList;
        final int size = tagList.size();

        for (int i = 0; i < size; i++) {
            NavigationButton navButon = null;
            NavTagBean navTagBean = tagList.get(i);
            BuildConfig.NativeSkip nativeSkip = null;
            if (BuildConfig.nativeMap.containsKey(navTagBean.navKey)) {
                nativeSkip = BuildConfig.nativeMap.get(navTagBean.navKey);
            }
            if (i == 0) {
                navButon = navBtn1;
            } else if (i == 1) {
                navButon = navBtn2;
            } else if (i == 2) {
                navButon = navBtn3;
            } else if (i == 3) {
                navButon = navBtn4;
            } else if (i == 4) {
                navButon = navBtn5;
            }

//            if (BuildCache.getAppConfigBean(mContext).navigationBean.navStyle == BuildConfig.NavStyle.PONIT_STYLE.getType()) {
//                //底部点聚最对4个tag 1.个iamgevie
//                if (i > 3) {
//                    return;
//                }
//            } else {
            //底部标签最对5个tag
            if (i > 4) {
                return;
            }
//            }

            if (nativeSkip != null) {
                buttonList.add(navButon);
                navButon.setVisibility(View.VISIBLE);
                navButon.setmUrl(navTagBean.navUrl);
                navButon.setTag(navTagBean);
                Class mCls = null;
                try {
                    mCls = Class.forName(nativeSkip.getFragmentClz());
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                navButon.init(navTagBean.iconName, navTagBean.navName, mCls);
            }
        }
    }

    PointDialog.IPointClickListener pointClickListener = new PointDialog.IPointClickListener() {
        @Override
        public void pointClick(NavTagBean poin_style) {
            BldNavigation.startNewModuleByKey(poin_style, mContext);
        }
    };

    private void showConfirmLogin() {
        Navigation.loginConfirm(mContext);
    }

    @Override
    public void onClick(View v) {
        if (!BaseApplication.mInstance.isAccountConfigured()) {
            showConfirmLogin();
            return;
        }
        if (v instanceof NavigationButton) {
            NavigationButton nav = (NavigationButton) v;
            doSelect(nav);
        }
//
//        else if (v.getId() == R.id.nav_item_img) {
//            L.v("img click ");
//            if (mDialog == null) {
//                mDialog = new PointDialog(mContext);
//                if (BuildCache.getAppConfigBean(mContext)
//                        .navigationBean.pointList == null || BuildCache.getAppConfigBean(mContext)
//                        .navigationBean.pointList.size() == 0) {
//                    T.showShort(mContext, "您未选择点聚应用。");
//                } else {
//                    mDialog.setPointList(BuildCache.getAppConfigBean(mContext)
//                            .navigationBean.pointList);
//                    mDialog.setPointClickListener(pointClickListener);
//                    mDialog.refreshView();
//                    mDialog.show();
//                }
//            } else {
//                mDialog.show();
//            }
//
//
////            TweetPublishActivity.show(getContext(), mRoot.findViewById(R.id.nav_item_tweet_pub));
//        }
    }

    public void setup(Context context, FragmentManager fragmentManager, int contentId, OnNavigationReselectListener listener) {
        mContext = context;
        mFragmentManager = fragmentManager;
        mContainerId = contentId;
        mOnNavigationReselectListener = listener;

        // do clear
        clearOldFragment();
        // do select first
        doSelect(navBtn1);
    }

    public void select(int index) {
        if (buttonList != null && buttonList.size() > index && buttonList.size() > 0) {
            doSelect(buttonList.get(index));
        }
    }

    private void clearOldFragment() {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        List<Fragment> fragments = mFragmentManager.getFragments();
        if (transaction == null || fragments == null || fragments.size() == 0)
            return;
        boolean doCommit = false;
        for (Fragment fragment : fragments) {
            if (fragment != this) {
                transaction.remove(fragment);
                doCommit = true;
            }
        }
        if (doCommit)
            transaction.commit();
    }

    private void doSelect(NavigationButton newNavButton) {
        NavigationButton oldNavButton = null;
        if (mCurrentNavButton != null) {
            oldNavButton = mCurrentNavButton;
            if (oldNavButton == newNavButton) {
                onReselect(oldNavButton);
                return;
            }
            oldNavButton.setSelected(false);
        }
        if (BuildConfig.MODULE_KEY_WNX.equals(((NavTagBean) newNavButton.getTag()).navKey)) {
            //微农信刷新列表
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put(EventBusConstant.TYPE, EventBusConstant.REFRESH_ADDRESSBOOK);
            EventBus.getDefault().post(dataMap);
        }
        newNavButton.setSelected(true);
        doTabChanged(oldNavButton, newNavButton);
        mCurrentNavButton = newNavButton;

    }

    private void doTabChanged(NavigationButton oldNavButton, NavigationButton newNavButton) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        if (oldNavButton != null) {
            if (oldNavButton.getFragment() != null) {
                ft.detach(oldNavButton.getFragment());
            }
        }
        if (newNavButton != null) {
            if (newNavButton.getFragment() == null) {
                Bundle mBundle = new Bundle();
                mBundle.putBoolean(BaseFragment.IS_SHOW_BACK, false);
                NavTagBean navTagBean = null;
                if (newNavButton.getTag() instanceof NavTagBean) {
                    navTagBean = (NavTagBean) newNavButton.getTag();
                }
                if (navTagBean == null) {
                    return;
                }
                if (BuildConfig.BROWSER_BUILD_URL.equals(navTagBean.navKey)) {
                    mBundle.putString(Constants.WEBVIEURL, WorkLogConstant.getMyWorkUrl(newNavButton.getmUrl() + "?&isShowBack=0"));
                    mBundle.putBoolean(BaseFragment.IS_SHOW_TOP, false);
                    if (TextUtils.isEmpty(navTagBean.navTitle)) {
                    } else {
                        mBundle.putString(Constants.TITITL, navTagBean.navTitle);
                    }
//                    mUrl = fragmentArgs.getString(Constants.WEBVIEURL);
//                    mTitle = fragmentArgs.getString(Constants.TITITL);
//                    mContent = fragmentArgs.getString(PARAM_CONTENT);
//                    org_id = fragmentArgs.getString(RequestConstant.KEY_ORG_ID);
                } else if (BuildConfig.BROWSER.equals(navTagBean.navKey)) {
                    mBundle.putString(Constants.WEBVIEURL, newNavButton.getmUrl());
                    mBundle.putBoolean(BaseFragment.IS_SHOW_TOP, true);
                    if (TextUtils.isEmpty(navTagBean.navName)) {
                        mBundle.putString(Constants.TITITL, navTagBean.navTitle);
                    } else {
                        mBundle.putString(Constants.TITITL, navTagBean.navName);
                    }
                } else {
                    mBundle.putBoolean(BaseFragment.IS_SHOW_TOP, true);
                }

                Fragment fragment = Fragment.instantiate(mContext,
                        newNavButton.getClx().getName(), mBundle);
                ft.add(mContainerId, fragment, navTagBean.navKey);

                newNavButton.setFragment(fragment);
            } else {

                ft.attach(newNavButton.getFragment());
            }
        }
        ft.commitAllowingStateLoss();
    }

    private void onReselect(NavigationButton navigationButton) {
        OnNavigationReselectListener listener = mOnNavigationReselectListener;
        if (listener != null) {
            listener.onReselect(navigationButton);
        }
    }

//    @Override
//    public void onNoticeArrived(NoticeBean bean) {
//        mNavMe.showRedDot(bean.getAllCount());
//    }

    public interface OnNavigationReselectListener {
        void onReselect(NavigationButton navigationButton);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }


}
